﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telecom.Data;
using Telecom.Service.EntityCustom;

namespace Telecom.Service
{
    public class vnptSiteSer
    {
        private readonly EntitiesTelecom dbContext = new EntitiesTelecom();
        private donviToSer _donviTo { get; set; }

        public vnptSiteSer()
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            dbContext.Configuration.ProxyCreationEnabled = false;
            _donviTo = new donviToSer();
        }

        public decimal addorUpdate(VNPT_SITE entity)
        {
            if (entity.SITEID == 0) dbContext.VNPT_SITE.Add(entity);
            else dbContext.Entry(entity).State = System.Data.EntityState.Modified;
            dbContext.SaveChanges();
            return entity.SITEID;
        }

        public List<VNPT_SITE> getListByLoaiSite(string loaiSite)
        {
            try
            {
                return dbContext.VNPT_SITE.Where(p => (p.LOAISITE == loaiSite || string.IsNullOrEmpty(loaiSite) == true)).ToList();
            }
            catch (Exception e)
            {
                List<VNPT_SITE> _listRe = new List<VNPT_SITE>();
                return _listRe;
            }
        }

        public ListObjectToJtable<VNPT_SITE> getList(string loaiSite, string[] donviQl, decimal userId, string type, int jtStartIndex = 0, int jtPageSize = 0)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            ListObjectToJtable<VNPT_SITE> _listReturnOjnect = new ListObjectToJtable<VNPT_SITE>();

            try
            {
              var _listReturnALl = dbContext.VNPT_SITE.Where(p => (p.LOAISITE == loaiSite || string.IsNullOrEmpty(loaiSite) == true)).ToList();
              if (donviQl != null)
                {
                    if (type == "dv")
                    {
                        var listToquan = _donviTo.ListArayTOByUser(userId);
                        _listReturnALl = _listReturnALl.Where(p => donviQl.Contains(p.DONVIQL) && listToquan.Contains(p.TOQL)).ToList();
                    }
                    else
                    {
                        _listReturnALl = _listReturnALl.Where(p => donviQl.Contains(p.TOQL)).ToList();
                    }
                }

                _listReturnOjnect.count = _listReturnALl.Count();

                if (jtPageSize != 0)
                {
                    _listReturnALl = _listReturnALl.Skip(jtStartIndex).Take(jtPageSize).ToList();
                }

                _listReturnOjnect.list = _listReturnALl;

                return _listReturnOjnect;
                
            }
            catch (Exception e)
            {
                ListObjectToJtable<VNPT_SITE> _listRe = new ListObjectToJtable<VNPT_SITE>();
                return _listRe;
            }
        }

        public List<VNPT_SITE> getListOnDVTOQLTRAMVT(string[] toql, string[] donviQl, string tramVT)
        {
            List<VNPT_SITE> _listReturnOjnect = new List<VNPT_SITE>();

            try
            {
                _listReturnOjnect = dbContext.VNPT_SITE.Where(p => toql.Contains(p.TOQL) && donviQl.Contains(p.DONVIQL)).ToList();

                _listReturnOjnect = (from i in _listReturnOjnect
                                    join k in dbContext.SITE_MANGVT on i.SITEID equals k.SITEID
                                    join l in dbContext.MANGVT on k.MANGVT equals l.MA
                                     where (l.MA == tramVT || string.IsNullOrEmpty(tramVT) == true)
                                    select i).ToList();

                return _listReturnOjnect;

            }
            catch (Exception e)
            {
                List<VNPT_SITE> _listRe = new List<VNPT_SITE>();
                return _listRe;
            }
        }

        public List<VNPT_SITE> getListOnDVTOQL(string[] toql, string[] donviQl)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            List<VNPT_SITE> _listReturnOjnect = new List<VNPT_SITE>();

            try
            {
                return dbContext.VNPT_SITE.Where(p => toql.Contains(p.TOQL) && donviQl.Contains(p.DONVIQL)).ToList();

            }
            catch (Exception e)
            {
                List<VNPT_SITE> _listRe = new List<VNPT_SITE>();
                return _listRe;
            }
        }

        public List<VNPT_SITE> getListOnDVTOQL_LoaiSite(string[] toql, string[] donviQl, string[] loaiSite)
        {
            dbContext.Configuration.LazyLoadingEnabled = false;
            List<VNPT_SITE> _listReturnOjnect = new List<VNPT_SITE>();

            try
            {
                return dbContext.VNPT_SITE.Where(p => toql.Contains(p.TOQL) && donviQl.Contains(p.DONVIQL) && loaiSite.Contains(p.LOAISITE)).ToList();

            }
            catch (Exception e)
            {
                List<VNPT_SITE> _listRe = new List<VNPT_SITE>();
                return _listRe;
            }
        }

        public VNPT_SITE GetById(int Id)
        {
            try
            {
                return dbContext.VNPT_SITE.Where(p => p.SITEID == Id).First();
            }
            catch (Exception ex)
            {
                VNPT_SITE _ml = new VNPT_SITE();
                return _ml;
            }
        }

        public VNPT_SITE GetByIdMa(string Id)
        {
            try
            {
                return dbContext.VNPT_SITE.Where(p => p.MA == Id).First();
            }
            catch (Exception ex)
            {
                VNPT_SITE _ml = new VNPT_SITE();
                return _ml;
            }
        }

        public bool deleteSite(int Id)
        {
            try
            {
                var _en = GetById(Id);
                dbContext.VNPT_SITE.Remove(_en);
                dbContext.SaveChanges();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool checkVnPTSites(string ma)
        {
            try
            {
                var _en = dbContext.VNPT_SITE.Where(p => p.MA.ToUpper() == ma).First();
                if (_en != null)
                    return true;
                else return false;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public List<VnptSiteLoadMaps> ViewVnptSiteOnmaps(decimal[] lopBanDo)
        {
            try
            {
                if(lopBanDo == null)
                {
                    List<VnptSiteLoadMaps> _returnNull = new List<VnptSiteLoadMaps>();
                    return _returnNull;
                }
                else
                {
                    List<VnptSiteLoadMaps> _returnList = new List<VnptSiteLoadMaps>();

                    var listLoaiSite = dbContext.LOAI_SITE.Where(p => lopBanDo.Contains(p.LOPBD.Value)).Select(p => p.MA).ToArray();
                    var itemSite = dbContext.VNPT_SITE.Where(p => listLoaiSite.Contains(p.LOAISITE)).ToList();
                    foreach (var item in itemSite)
                    {
                        try
                        {
                            VnptSiteLoadMaps _model = new VnptSiteLoadMaps();
                            _model._entity = item;
                            _model.images = dbContext.LOAI_SITE.Where(p => p.MA == item.LOAISITE).First().MARKER;

                            _returnList.Add(_model);
                        }
                        catch (Exception ex) { continue; }

                    }

                    return _returnList;

                }
            }
            catch (Exception ex)
            {
                List<VnptSiteLoadMaps> _return = new List<VnptSiteLoadMaps>();
                return _return;
            }
        }

        public List<VnptSiteLoadMaps> ViewVnptSiteOnmaps_ks(string loaiSite)
        {
            try
            {
                List<VnptSiteLoadMaps> _returnList = new List<VnptSiteLoadMaps>();

                var itemSite = dbContext.VNPT_SITE.Where(p => p.LOAISITE == loaiSite).ToList();
                foreach (var item in itemSite)
                {
                    try
                    {
                        VnptSiteLoadMaps _model = new VnptSiteLoadMaps();
                        _model._entity = item;
                        _model.images = dbContext.LOAI_SITE.Where(p => p.MA == item.LOAISITE).First().MARKER;

                        _returnList.Add(_model);
                    }
                    catch (Exception ex) { continue; }

                }

                return _returnList;
            }
            catch (Exception ex)
            {
                List<VnptSiteLoadMaps> _return = new List<VnptSiteLoadMaps>();
                return _return;
            }
        }

    }
}
